package com.shiguiwu.springboot3.rabbitmq.controller;

import com.shiguiwu.springboot3.dto.UserDTO;
import com.shiguiwu.springboot3.rabbitmq.config.CorrelationDataExt;
import com.shiguiwu.springboot3.util.R;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 控制层
 * @author: stone
 * @date: Created by 2023/11/17 21:57
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.rabbitmq.controller
 */
@RestController
@RequestMapping("/rabbitmq")
public class RabbitMQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送成功的，
     * @param num
     * @param username
     * @return
     */
    @GetMapping("/sendSuccess")
    public R<String> sendSuccess(@RequestParam(defaultValue = "1") Integer num, @RequestParam String username) {

        for (int i = 0; i < num; i++) {
            UserDTO userDTO = new UserDTO();
            userDTO.setId(i);
            userDTO.setUsername(username);
            userDTO.setPassword("aaa");
            rabbitTemplate.convertAndSend("test_ex", "test_key.aaa", userDTO);
            //测试使用字符串是否可以，答案是不可以
            //rabbitTemplate.convertAndSend("test_ex", "test_key.aaa", JSONUtil.toJsonStr(userDTO));
        }
        return R.ok("发送成功");
    }

    /**
     * 发送失败
     * 模拟rabbitmq发送成功，但是未进行ack,当达到        prefetch * 节点数时，消息队列会阻塞
     * @param num
     * @param username
     * @return
     */
    @GetMapping("/sendError")
    public R<String> sendError(@RequestParam(defaultValue = "1") Integer num, @RequestParam String username) {

        for (int i = 0; i < num; i++) {
            UserDTO userDTO = new UserDTO();
            userDTO.setId(-1);
            userDTO.setUsername(username);
            userDTO.setPassword("aaa");
            rabbitTemplate.convertAndSend("test_ex", "test_key", userDTO);
        }
        return R.ok("失败");
    }

    /**
     * 发送失败
     * 模拟消息未到达交换机
     * @param num
     * @param username
     * @return
     */
    @GetMapping("/sendErrorExchange")
    public R<String> sendErrorExchange(@RequestParam(defaultValue = "1") Integer num, @RequestParam String username) {

        for (int i = 0; i < num; i++) {
            UserDTO userDTO = new UserDTO();
            userDTO.setId(-1);
            userDTO.setUsername(username);
            userDTO.setPassword("aaa");
            CorrelationDataExt<UserDTO> correlationData = new CorrelationDataExt<>();
            correlationData.setId(String.valueOf(userDTO.getId()));
            correlationData.setData(userDTO);
            rabbitTemplate.convertAndSend("test_ex1122211", "test_key11", userDTO, correlationData);
        }
        return R.ok("失败");
    }

    /**
     * 模拟消息队列未达到队列
     * @param num
     * @param username
     * @return
     */
    @GetMapping("/sendErrorQueue")
    public R<String> sendErrorQueue(@RequestParam(defaultValue = "1") Integer num, @RequestParam String username) {
        for (int i = 0; i < num; i++) {
            UserDTO userDTO = new UserDTO();
            userDTO.setId(-1);
            userDTO.setUsername(username);
            userDTO.setPassword("aaa");
            rabbitTemplate.convertAndSend("test_ex11", "test_key.test", userDTO);
        }
        return R.ok("失败");
    }


    /**
     * 发送死信队列信息
     * @param num
     * @param username
     * @return
     */
    @GetMapping("/sendDeadQueue")
    public R<String> sendDeadQueue(@RequestParam(defaultValue = "1") Integer num, @RequestParam String username) {
        for (int i = 0; i < num; i++) {
            UserDTO userDTO = new UserDTO();
            userDTO.setId(i);
            userDTO.setUsername(username);
            userDTO.setPassword("死信队列消息");
            rabbitTemplate.convertAndSend("test_ex", "shiguiwu_key.1", userDTO);
        }
        return R.ok("失败");
    }

}
